<?php 
/***
    Amrita ITEWS - Copyright (C) 2009 Amrita Vishwa Vidyapeetham, Amritapuri.
                                     (http://www.amrita.edu)
    ***************************************************************************
    This file is part of the Amrita ITEWS distribution.
    Amrita ITEWS is free software; you can redistribute it and/or modify it 
    under the terms of the GNU General Public License (version 2) as published 
    by the Free Software Foundation AND MODIFIED BY the Amrita ITEWS exception.
    ***NOTE*** The exception to the GPL is included to allow you to distribute
    a combined work that includes Amrita ITEWS without being obliged to provide
    the source code for proprietary components outside of the Amrita ITEWS
    software. Amrita ITEWS is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    Public License for more details. You should have received a copy of the GNU
    General Public License and the Amrita ITEWS license exception along with 
    Amrita ITEWS if not then it can be viewed here: 
    http://itews.amrita.ac.in/license.html.
    
    Documentation, latest information, license and contact details are at:
    http://itews.amrita.ac.in/
 
    Amrita ITEWS source code can be found at:
    http://code.google.com/p/itews"; 
    
    The initial code-base of Amrita ITEWS was developed by Amrita Vishwa 
    Vidyapeetham as part of the project titled,\"Intelligent & Interactive 
    Telematics using Emerging Wireless Technologies for Transport Systems 
    (ITEWS)\" sponsored by Technology Information Forecasting and Assessment 
    Council (TIFAC), India.
***/
?><?php
class CenterToDmuMessageInfoController extends AppController {
	
	var $name = 'CenterToDmuMessageInfos'; 
	var $uses = array('CenterToDmuMessage', 'Message', 'TelematicUnit');
	var $components = array('Spyc'); 
	
	function beforeFilter() {
		
		// Set Timezone
		date_default_timezone_set("Asia/Calcutta");
		$this->setAutoIncremetsForModelObjects();
		
	}

    function index() {
    	
    	$this->CenterToDmuMessage->begin();
    	$allowCommit  = true;
    	$errorMessage = ""; 
    	
		// Getting GET header if any
		if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) {		
			$lastProcessedInfoId  = intval($_SERVER['HTTP_IF_NONE_MATCH']);
		} else {
			$lastProcessedInfoId  = 0;
		}
		
		// Checking validity of  If-None-Match header
		if (!ereg("[[:digit:]]", $lastProcessedInfoId)) {
				// Invalid request; HTTP If-None-Match header contains invalid data	
				header("HTTP/1.1 404 Not Found");
				$allowCommit  = false;
				$responce = "Invalid request\n";	
				echo $responce;
				//$this->render('../webservices/index', 'webservice');
				exit(0);
		}
		
		// Delete all entries in the poi info table with id <= $lastProcessedInfoId
		if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) {		
			$oldEntries = $this->CenterToDmuMessage->findAll('CenterToDmuMessage.id <='.$lastProcessedInfoId, 'CenterToDmuMessage.id');
			foreach($oldEntries as $oldEntry){
				if (!$this->CenterToDmuMessage->del($oldEntry['CenterToDmuMessage']['id'])) {
					$allowCommit  = false;
					$responce = "Error while deleting old info entries.\n";
					echo $responce;
					//$this->render('../webservices/index', 'webservice');
					exit(0);
				}
			}
		}
    		
	    // Commit database changes
		if ($allowCommit) {
			$this->CenterToDmuMessage->commit();
		} else {
			$this->CenterToDmuMessage->rollback();
			$responce = "Error.\n";
			echo $responce;
			exit(0);
		}
		
		$nextInfo = $this->CenterToDmuMessage->find();
		
		// Return nothing if no poi info table entries are present
		if ($nextInfo == null) {
			// No updates, empty database table		
			header("HTTP/.1 304 Not Modified");
			$responce = "No updates.\n";
			echo $responce;
			//$this->render('../webservices/index', 'webservice');
			exit(0);
		}
    	
		// Create a new info responce
		$nextInfoId              = $nextInfo['CenterToDmuMessage']['id'];
		$centerToDmuMessageInfo['info_id']   = $nextInfoId;
		$centerToDmuMessageInfo['acivation_time']  = $nextInfo['CenterToDmuMessage']['message_activation_time'];
		$centerToDmuMessageInfo['expiry_time']     = $nextInfo['CenterToDmuMessage']['message_expiry_time'];
		$centerToDmuMessageInfo['msg']             = $nextInfo['Message']['message_content'];
		
		// To get ivtu list
		$tuList = "";
		if (array_key_exists('TelematicUnit', $nextInfo)) {
			$tuList = $nextInfo['TelematicUnit']['telematic_unit_code'];
		}
		
		// Dumping into YAML format		    	
		$responce = $this->Spyc->YamlDump($centerToDmuMessageInfo);
		
		// Since there is no explicit render function call and we are not loggedin user will be redirected to login screen
		// So afterFilter will not be called automatically 
		$this->afterFilter();
		
		// Set Headers
		header("Cache-Control: no-cache");
		header("Pragma: no-cache");
		header("ETag:" . $nextInfoId);
		header("X-TU-List:" . $tuList);
		
		// Render output
		$this->set('responce', $responce);
		echo $responce;	
		exit(1);
		
    }
}
?>